package com.gs.api.projects.bzapplets.web;

import com.github.pagehelper.PageInfo;
import com.gs.api.projects.bzapplets.entity.BzBaseInfo;
import com.gs.api.projects.bzapplets.entity.BzFavorite;
import com.gs.api.projects.bzapplets.entity.BzWallpaper;
import com.gs.api.projects.bzapplets.service.BzBaseInfoService;
import com.gs.api.projects.bzapplets.service.BzFavoriteService;
import com.gs.api.projects.bzapplets.service.BzWallpaperService;
import com.gs.api.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guoyunfei on 2021/7/9 14:04
 * @className BzWallpaperDao
 * @description TODO
 * @Version 1.0
 */
@Api(tags = "微信壁纸信息")
@RestController
@RequestMapping("{appid}/wallpaper")
public class BzWallpaperController {

    private final String ORDER_BY_ID = "id desc";
    private final String ORDER_BY_BROEWSE_COUNT = "browse_count desc";
    private final String ORDER_BY_LIKE_COUNT = "like_count desc";
    private final String ORDER_BY_COLLECTION_COUNT = "collection_count desc";

    @Autowired
    private BzWallpaperService bzWallpaperService;

    @Autowired
    private BzFavoriteService bzFavoriteService;

    @Autowired
    private BzBaseInfoService bzBaseInfoService;

    /**
     * 设置排序
     * @param order
     * @return
     */
    public String getOrderBy(Integer order){
        if(null == order){
            return ORDER_BY_ID;
        }
        if(0 == order){
            return ORDER_BY_BROEWSE_COUNT;
        }else if(1 == order){
            return ORDER_BY_LIKE_COUNT;
        }else if(2 == order){
            return ORDER_BY_COLLECTION_COUNT;
        }
        return ORDER_BY_ID;
    }

    @ApiOperation(value = "壁纸列表", notes="壁纸列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码，默认1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数，默认12", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "order", value = "排序字段0浏览数1点赞数2收藏数", required = false, dataType = "String"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/list")
    private Result wallpaperList(BzWallpaper wallpaper,Integer pageNum,Integer pageSize,Integer order,@PathVariable String appid){

        if(null == pageNum){
            pageNum = 1;
        }
        if(null == pageSize){
            pageSize = 12;
        }
        wallpaper.setAppid(appid);
        //排序
        String orderBy = getOrderBy(order);
        PageInfo<BzWallpaper> pageInfo = bzWallpaperService.pageInfo(wallpaper, pageNum, pageSize,orderBy);
        List<BzWallpaper> list = pageInfo.getList();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            //String value = config.get(0).getValue();
            list.get(i).setThumbnail(list.get(i).getOssResourcesurl() + list.get(i).getThumbnail());
        }
        pageInfo.setList(list);
        return Result.success(pageInfo);
    }

    /**
     * 查询图片详情
     * @param id 壁纸id
     * @param openid 用户id
     * @return
     */
    @ApiOperation(value = "查看壁纸详情", notes="查看壁纸详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "壁纸ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/data.json")
    public Result get(Integer id,String openid,@PathVariable String appid){
        if(null == id){
            return Result.failure("获取参数失败");
        }

        Map<String,Object> map = new HashMap<>(6);

        BzWallpaper bzWallpaper = bzWallpaperService.getById(id);

        map.put("bzWallpaper",bzWallpaper);

        BzBaseInfo info = bzBaseInfoService.selectOne(appid);
        //工具栏
        map.put("switch_comment",info.getIsTools());
        //小程序名称
        map.put("title",info.getAppName());

        //查询用户是否收藏此 查询类型为2的
        if(StringUtils.isNotBlank(openid)){
            BzFavorite collection = new BzFavorite();
            collection.setOpenid(openid);
            collection.setWallpaperId(id);
            collection.setType(1);
            collection.setAppid(appid);
            map.put("post_favorite", bzFavoriteService.selectCount(collection));
            collection.setType(2);
            map.put("post_like", bzFavoriteService.selectCount(collection));
        }

        return Result.success(map);
    }


    @ApiOperation(value = "修改用户浏览，点赞，收藏", notes="修改用户浏览，点赞，收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "壁纸ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "flag", value = "标记，1浏览数2下载3分享4收藏5点赞6减少收藏7减少点赞", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("updateCount")
    public Result updateCount(Integer id, Integer flag, String openid,@PathVariable String appid){

        Map<String,Object> map = new HashMap<>(6);
        map.put("id",id);
        map.put("appid",appid);

        BzFavorite collection = new BzFavorite();
        collection.setWallpaperId(id);
        collection.setAppid(appid);

        if(1 == flag){
            //浏览数
            bzWallpaperService.updateBrowseCount(map);

            //增加个人浏览数
            if(StringUtils.isNotBlank(openid)){
                //查询用户是否已经存在浏览记录
                collection.setOpenid(openid);
                collection.setType(3); //1收藏2点赞3浏览
                int i = bzFavoriteService.selectCount(collection);
                if(i <= 0){
                    collection.setCreateTime(new Date());
                    bzFavoriteService.save(collection);
                }

            }
        }else if(2 == flag){
            //下载数
            bzWallpaperService.updateDownCount(map);
        }else if(3 == flag){
            //分享数
            bzWallpaperService.updateShareCount(map);
        }else if(4 == flag){
            //收藏数
            bzWallpaperService.updateCollectionCount(map);
        }else if(5 == flag){
            //点赞数量
            bzWallpaperService.updateLikeCount(map);
        }else if(6 == flag){
            bzFavoriteService.deleteByWallpaperId(id,openid,1,appid);
            //减少收藏数量
            bzWallpaperService.updateSubCollectionCount(map);
        }else if(7 == flag){
            bzFavoriteService.deleteByWallpaperId(id,openid,2,appid);
            //减少点赞数量
            bzWallpaperService.updateSubLikeCount(map);
        }

        if(4 == flag || 5 == flag){
            if(StringUtils.isNotBlank(openid)){
                collection.setOpenid(openid);
                collection.setType(4 == flag ? 1 : 2);  //1收藏2点赞
                collection.setCreateTime(new Date());
                bzFavoriteService.save(collection);
            }
        }
        return Result.success();
    }


    @ApiOperation(value = "左滑右滑获取信息", notes="左滑右滑获取信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "壁纸ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "flag", value = "value=left，right", required = true, dataType = "String"),
            @ApiImplicitParam(name = "openid", value = "用户openid", required = false, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = false, dataType = "String"),
    })
    @GetMapping("upAndDownData")
    public Result upAndDownData(@PathVariable String appid, Integer id,String createTime,String flag,String openid){
        if(null == id){
            return Result.failure("获取参数失败");
        }

        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("flag",flag);
        map.put("appid",appid);
        BzWallpaper wallpaper = bzWallpaperService.upAndDownData(map);


        if(null == wallpaper){
            return Result.failure("没有了哦");
        }

        Map<String,Object> resultMap = new HashMap<>(6);
        resultMap.put("bzWallpaper",wallpaper);

        //查询用户是否收藏此 查询类型为2的
        if(StringUtils.isNotBlank(openid)){
            BzFavorite collection = new BzFavorite();
            collection.setOpenid(openid);
            collection.setWallpaperId(wallpaper.getId());
            collection.setAppid(appid);
            collection.setType(1);
            resultMap.put("post_favorite", bzFavoriteService.selectCount(collection) > 0 ? 1 : 0);
            collection.setType(2);
            resultMap.put("post_like", bzFavoriteService.selectCount(collection)  > 0 ? 1 : 0);
        }

        BzBaseInfo info = bzBaseInfoService.selectOne(appid);
        resultMap.put("switch_comment",info.getIsTools());
        resultMap.put("title",info.getAppName());

        return Result.success(resultMap);
    }



    // TODO  壁纸列表版本 new版，带采集数据
    @ApiOperation(value = "壁纸列表新版-带采集数据", notes="壁纸列表新版-带采集数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码，默认1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数，默认12", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "order", value = "排序字段0浏览数1点赞数2收藏数", required = false, dataType = "String"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/newList")
    private Result newWallpaperList(BzWallpaper wallpaper,Integer pageNum,Integer pageSize,Integer order,@PathVariable String appid){

        if(null == pageNum){
            pageNum = 1;
        }
        if(null == pageSize){
            pageSize = 12;
        }
        wallpaper.setAppid(appid);
        //排序
        String orderBy = getOrderBy(order);
        PageInfo<BzWallpaper> pageInfo = bzWallpaperService.pageInfoNew(wallpaper, pageNum, pageSize,orderBy);
        List<BzWallpaper> list = pageInfo.getList();
        pageInfo.setList(list);
        return Result.success(pageInfo);
    }


}
